package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.DepartureOrderInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Departure fragment.
 *
 * @author sHadowLess
 */
public class DepartureFragment extends BaseFragment {

    /**
     * The Departure order no.
     */
    @BindView(R.id.departure_orderNo)
    TextView departureOrderNo;
    /**
     * The Departure make date.
     */
    @BindView(R.id.departure_makeDate)
    TextView departureMakeDate;
    /**
     * The Departure marker.
     */
    @BindView(R.id.departure_marker)
    TextView departureMarker;
    /**
     * The Departure app info applier.
     */
    @BindView(R.id.departure_appInfo_applier)
    TextView departureAppInfoApplier;
    /**
     * The Departure apply depart.
     */
    @BindView(R.id.departure_applyDepart)
    TextView departureApplyDepart;
    /**
     * The Departure position name.
     */
    @BindView(R.id.departure_positionName)
    TextView departurePositionName;
    /**
     * The Departure birth day.
     */
    @BindView(R.id.departure_BirthDay)
    TextView departureBirthDay;
    /**
     * The Departure date of entry.
     */
    @BindView(R.id.departure_dateOfEntry)
    TextView departureDateOfEntry;
    /**
     * The Departure leave reason.
     */
    @BindView(R.id.departure_leaveReason)
    TextView departureLeaveReason;
    /**
     * The Departure order leave date.
     */
    @BindView(R.id.departure_orderLeaveDate)
    TextView departureOrderLeaveDate;
    /**
     * The Departure leaving reason.
     */
    @BindView(R.id.departure_LeavingReason)
    TextView departureLeavingReason;
    /**
     * The Departure suggest.
     */
    @BindView(R.id.departure_Suggest)
    TextView departureSuggest;
    /**
     * The Departure check process.
     */
    @BindView(R.id.departure_check_process)
    LinearLayout departureCheckProcess;

    private final String url = "api/UserResignApply/LoadCheckHistoryList?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_departure_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initDepartureOrderInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "orderId") + orderId, mActivity, departureCheckProcess);
        }
    }

    private void initDepartureOrderInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getDepartureOrderInfo(orderId, userName, new RequestCallBack<DepartureOrderInfo>() {
                    @Override
                    public void success(RequestResult<DepartureOrderInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                DepartureOrderInfo.DataBean dataBean = result.getResult().getData();
                                DepartureOrderInfo.DataBean.ApplicantObjectBean applicantObjectBean = dataBean.getApplicantObject();
                                String id = dataBean.getId();
                                String createTimeString = dataBean.getCreateTimeString();
                                String createUserName = dataBean.getCreateUserName();
                                departureOrderNo.setText(id);
                                departureMakeDate.setText(createTimeString);
                                departureMarker.setText(createUserName);

                                String applicantName = applicantObjectBean.getUserName();
                                String departmentName = applicantObjectBean.getDepartmentName();
                                String positionName = applicantObjectBean.getPositionName();
                                String birthdayStr = applicantObjectBean.getBirthdayStr();
                                String inDateStr = applicantObjectBean.getInDateStr();
                                departureAppInfoApplier.setText(applicantName);
                                departureApplyDepart.setText(departmentName);
                                departurePositionName.setText(positionName);
                                departureBirthDay.setText(birthdayStr);
                                departureDateOfEntry.setText(inDateStr);

                                String resignTypeName = dataBean.getResignTypeName();
                                String resignDateString = dataBean.getResignDateString();
                                String reason = dataBean.getReason();
                                String remark = dataBean.getRemark();
                                departureLeaveReason.setText(resignTypeName);
                                departureOrderLeaveDate.setText(resignDateString);
                                departureLeavingReason.setText(reason);
                                departureSuggest.setText(remark);
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
